<!-- 知识产权页面 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kjy:application:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:application:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="intellectualList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="专利名称" align="center" prop="name" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="专利类型" align="center" prop="type" />
      <el-table-column label="附件" align="center" prop="certificate">
        <template slot-scope="scope">
          <a
            v-if="scope.row.certificate !== null"
            :href="getFirstItem(scope.row.certificate)"
            target="_blank"
            >{{ FormatFilename(getFirstItem(scope.row.certificate)) }}</a
          >
        </template>
      </el-table-column>

      <el-table-column label="专利号" align="center" prop="certificateNumber">
      </el-table-column>
      <el-table-column label="获得时间" align="center" prop="certificateDate">
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="publishTime" />
      <!-- <el-table-column label="经费" align="center" prop="funding" />
      <el-table-column label="申请人id" align="center" prop="applicantId" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="专利名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入专利名称" />
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input
            v-model="form.enterpriseName"
            placeholder="请输入企业名称"
          />
        </el-form-item>
        <el-form-item label="专利类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择专利类型">
            <el-option
              v-for="item in types"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="专利编号" prop="certificateNumber">
          <el-input
            v-model="form.certificateNumber"
            placeholder="请输入专利编号"
          />
        </el-form-item>
        <el-form-item label="获取日期" prop="certificateDate">
          <el-date-picker
            v-model="form.certificateDate"
            type="date"
            placeholder="选择获取专利日期"
          />
        </el-form-item>
        <el-form-item label="上传附件" prop="certificate">
          <file-upload
            :fileType="['doc', 'docx', 'pdf', 'png', 'png', 'jpg', 'jpeg']"
            v-model="form.certificate"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApplication,
  getApplication,
  delApplication,
  addApplication,
  updateApplication,
  adminAddApplication,
  listEnterprise,
} from "@/api/system/storage-application";
import {
  getIntellectualProperty,
  getPatentType,
  addInfo,
  updateInfo,
  delInfo,
} from "../../../api/system/intellectual-property";
export default {
  name: "Application",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        enterpriseName: null,
      },
      // 表单参数
      form: {
        id: "",
        name: "",
        enterpriseName: "",
        type: null, //  1 发明专利/ 2 实用新型/3 软著
        certificateNumber: "",
        certificate: "",
        certificateDate: "",
        publishTime: "",
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "专利名称不能为空", trigger: "blur" },
        ],
        enterpriseName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "专利类型不能为空", trigger: "blur" },
        ],
        certificateNumber: [
          { required: true, message: "专利号不能为空", trigger: "blur" },
        ],
        certificateDate: [
          { required: true, message: "获取日期不能为空", trigger: "blur" },
        ]
      },

      //企业
      // 专利表格数据
      intellectualList: [],
      //企业选择项
      selectedItem: null,
      //专利类型
      types: [],
      searchText: null,
      isLoading: false,
    };
  },
  mounted() {
    this.getList();
    getPatentType().then((response) => {
      this.types = response.data;
    });
  },
  methods: {
    /** 查询专利列表 */
    getList() {
      this.loading = true;
      getIntellectualProperty(this.queryParams).then((response) => {
        this.intellectualList = response.data.records;
        //console.log(JSON.stringify(this.intellectualList));
        this.total = response.data.total;
        this.queryParams.current = response.data.current;
        this.loading = false;
      });
    },
    getFirstItem(item) {
      if (item !== null && typeof item === "string") {
        const items = item.split(",");
        if (items.length > 0) {
          return items[0].trim();
        }
      }
      return item;
    },

    handleSelectChange() {
      if (form.enterpriseName) {
        const selectedOption = this.filteredItems.find(
          (item) => item.id === this.selectedItem
        );
        form.enterpriseName = selectedOption ? selectedOption.name : ""; // 根据选中的 item.id 查找对应的 item.name，并赋值给 searchText
      }
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: "",
        enterpriseName: "",
        type: "1", //  1 发明专利/ 2 实用新型/3 软著
        certificateNumber: "",
        certificate: "",
        certificateDate: "",
        publishTime: "",
      };
      this.selectedItem = null;
      this.searchText = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加知识产权";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row
      this.open=true
      this.title = "修改知识产权"
    },
    /** 提交按钮 */

    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.publishTime = new Date().toISOString();
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除选中数据项？")
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
